﻿<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
    <script src="/Style/JS/Aries.Loader.js"></script>
</head>
<body>
    <div class="page-title">
        <h3>${rightKey}${config}${menu}</h3>
        <a class="arrows hover"></a>
    </div>
    <div class="page-content">
        <div style="width: 100%; float: left">
            <div style="width: 20%; float: left;">
                <ul id="ul_menus"></ul>

            </div>
            <div style="width: 80%; float: left">
                <div class="page-form">
                    <form id="form1">
                        <div class="line">
                            <label><em>*</em>${name}：</label>
                            <input name="menuname" id="menuname" data-options="required:true" class='easyui-validatebox' style="width: 250px;" />
                        </div>
                        <div class="line">
                            <label><em>*</em>${url}：</label>
                            <input name="menuurl" id="menuurl" data-options="required:true" class='easyui-validatebox' style="width: 250px;" />
                        </div>
                        <div class="line">
                            <label><em>*</em>${sortOrder}：</label>
                            <input name="sortorder" id="sortorder" data-options="required:true" class='easyui-numberbox' style="width: 250px;" />
                        </div>
                        <div class="line">
                            <label><em>*</em>${isShow}：</label>
                            <div class="formRad">
                                <input type="radio" name="isShow" id="rdo_yes" value="1" /><span>${yes}</span><input type="radio" name="isShow" id="rdo_no" checked="checked" value="0" /><span>${no}</span>
                            </div>
                        </div>
                        <div class="line">
                            <label><em>*</em>${permission}：</label>
                            <div class="formRad" id="permission"></div>
                        </div>
                        <div class="btn">
                            <a>
                                <input class="submit" type="button" name="${save}" value="" />
                            </a>
                            <!--    <a>
                                <input id="btn_cancel" class="return" type="reset" name="重置" value="" />
                            </a>-->
                        </div>
                        <input type="hidden" name="menuid" id="menuid" value="" />
                        <input type="hidden" name="actionids" id="actionids" value="" />
                        <input type="hidden" name="parentmenuid" id="parentmenuid" value="" />
                    </form>
                </div>
            </div>
        </div>
    </div>
    <div style="clear: both;">
        <div id="mm" class="easyui-menu" style="width: 120px;">
            <div data-options="iconCls:'icon-add'" id="btnAdd">${add}${page}</div>
            <div data-options="iconCls:'icon-remove'" id="btnDelete">${delete}${page}</div>
        </div>
        <div id="win"></div>
    </div>
</body>
</html>
<script>
    (function (Menu) {
        Menu.Tree = {
            $target: $("#ul_menus"),
            //当前点击的节点
            clickNode: undefined,
            //添加的菜单层级
            level: 0,
            getSelected: function () { return Menu.Tree.$target.tree("getSelected"); },
            //初始化菜单
            onInit: function () {
                var that = this;
                AR.Ajax.post("GetMenu", "Sys_Menu", {},
                  function (data) {
                      $("#ul_menus").tree({
                          idField: "menuid",
                          textField: "menuname",
                          rootText: "${root}",
                          //rootid: undefined,
                          parentField: "parentmenuid",
                          data: data,
                          onContextMenu: function (e, node) {
                              e.preventDefault();
                              $(this).tree('select', node.target);
                              $('#mm').menu('show', {
                                  left: e.pageX,
                                  top: e.pageY
                              });
                          },
                          onClick: that.onClick
                      });

                  });
                this.regEvent();
            },
            //树节点单击
            onClick: function (node) {
                if (node == undefined) {
                    node = Menu.Tree.getSelected();
                }

                if (node != undefined && node.id != null) {
                    $("#menuid").val(node.id);
                    Menu.Form.fill(node.id);
                }
                else {
                    $("#form1").form("clear");
                }

                Menu.Tree.clickNode = node;
                AR.Global.DG.action = "Update";
            },
            regEvent: function () {
                var that = this;
                //注册右键菜单事件
                $("#btnAdd").click(function () { that.ContextMenu.onAdd(); });
                $("#btnDelete").click(function () { that.ContextMenu.onDel(); });
            },
            ContextMenu: {
                //新增页面
                onAdd: function () {
                    Menu.Form.fill();
                },
                //删除菜单
                onDel: function () {
                    var that = this;
                    var node = Menu.Tree.getSelected();
                    if (node != undefined) {
                        if (node.id == undefined)
                        {
                            AR.Window.showMsg("${root}${notAllow}${delete}！");
                            return false;
                        }
                        var msg = "${confirm}${delete}?";
                        var isLeaf = Menu.Tree.$target.tree("isLeaf", node.target);
                        if (!isLeaf) {
                            msg = node.text + " ${confirmAgain}";
                        }
                        AR.Window.confirm(msg, "${prompt}", function () {
                            AR.Ajax.post("DeleteMenu", "Sys_Menu", { id: node.id },
                            function (data) {
                                if (data.success) {
                                    var parentNode = Menu.Tree.$target.tree("getParent", node.target);
                                    Menu.Tree.$target.tree("remove", node.target);
                                    Menu.Tree.$target.tree("select", parentNode.target);
                                    Menu.Tree.clickNode = parentNode;
                                    AR.Global.DG.action = "Add";
                                }
                                AR.Window.showMsg(data.msg);

                            });
                        });
                    } else {
                        AR.Window.showMsg("${selectOne}");
                    }
                }
            }
        };
        Menu.Form = {
            //功能权限数据
            actionData: undefined,

            //加载权限
            onInit: function () {
                var that = this;
                AR.Ajax.post("GetActions", "Sys_Action", {}, function (data) {
                    that.actionData = data;
                    that.createCheckbox();
                });
                this.regEvent();
            },
            createCheckbox: function () {
                var that = this;
                if (this.actionData != undefined) {
                    //清除权限模块
                    $("#permission").html("");
                    $.each(this.actionData, function (i, dom) {
                        $("#permission").append('<input type="checkbox"  ref="' + dom.actionrefname + '" value="' + dom.actionid + '"/>' + dom.actionname + '');
                    });
                }
                //每次选中之后帮隐藏表单赋值
                $("#permission :checkbox").bind("click", function () {
                    that.onClickCheckbox();
                });
            },
            //获取选中的权限id
            onClickCheckbox: function () {
                $("#actionids").val("");
                var ids = [];
                $("#permission [type='checkbox']:checked").each(function (i, dom) {
                    ids.push($(this).val());
                });
                if (ids.length > 0) {
                    $("#actionids").val(ids.join(','));
                }
            },
            //加载编辑框
            fill: function (menuid) {
                var that = this;
                if (menuid != undefined) {
                    $("#form1").form("clear");
                    AR.Ajax.post("GetMenuDetails", "Sys_Menu", { id: menuid },
                     function (data) {
                         $("#form1").form('load', data);
                         that.setAction(data.actionids);
                         $("#menuname").blur();
                     });
                }
                else {
                    $("#menuid").val("");
                    var node = Menu.Tree.getSelected();;
                    if (node) {
                        //帮隐藏表单菜单id赋值
                        $("#parentmenuid").val(node.id);
                        AR.Global.DG.action = "Add";
                    }
                    Menu.Tree.clickNode = node;
                    //初始一些默认值
                    $("#menuname").val("");
                    $("#menuurl").val("#");
                    $('#sortorder').numberbox('setValue', '100');
                    $(':radio[name="isShow"]').eq(0).attr("checked", true);
                    if (node.id != undefined) {
                        $("#menuurl").val("/Web/SysAdmin/CustomView.html?objname=");
                        var defaultActionIDs = "";
                        for (var i = 0; i < this.actionData.length; i++) {
                            switch (this.actionData[i].actionrefname) {
                                case "add":
                                case "edit":
                                case "del":
                                case "view":
                                case "import":
                                case "export":
                                case "config":
                                    if (defaultActionIDs == "") { defaultActionIDs = this.actionData[i].actionid; }
                                    else
                                    {
                                        defaultActionIDs = defaultActionIDs + "," + this.actionData[i].actionid;
                                    }
                                    break;
                            }
                        }
                        that.setAction(defaultActionIDs);
                    }
                }

            },
            //设置权限选中
            setAction: function (actionids) {
                this.createCheckbox();//重新创建，等于重置状态
                if (actionids != null && actionids != undefined) {
                    var arr = actionids.toLocaleLowerCase().split(',');
                    for (var i = 0; i < arr.length; i++) {
                        $("#permission :checkbox").each(function (k, dom) {
                            if (arr[i] == $(this).val()) {
                                $(this).attr("checked", "checked");
                            }
                        });
                    }
                    //编辑页加载 设置选中之后帮隐藏表单赋值
                    $("#actionids").val(actionids);
                }
            },
            regEvent: function () {
                var that = this;
                AR.Form.BtnCommit.onBeforeExecute = function (para) {
                    var action = AR.Global.DG.action;
                    AR.Form.action = action;
                    AR.Form.objName = "Sys_Menu";

                    var node = Menu.Tree.getSelected();;
                    Menu.Tree.level = 0;
                    if (action == "Add") {
                        if (para.menuid) {
                            delete para.menuid;
                        }
                        if (Menu.Tree.clickNode.attributes == null) {
                            Menu.Tree.level = 1;
                            para.menulevel = 1;

                        }
                        else {
                            var level = Menu.Tree.clickNode.attributes.menulevel || 0;
                            Menu.Tree.level = level + 1;
                            para.menulevel = level + 1;

                        }
                    }
                    else {
                        if (!para.menuid && (node == null || node.id == null)) {
                            AR.Window.showMsg("${root}${notAllow}${update}！");
                            return false;
                        }
                        if (para.parentmenuid == "") {
                            delete para.parentmenuid;
                        }
                    }
                };
                AR.Form.BtnCommit.onAfterExecute = function (result, formData) {
                    if (!result.success) {
                        AR.Window.alert(result.msg);
                        return false;
                    }
                    var action = AR.Global.DG.action
                    if (action == "Add") {
                        Menu.Tree.$target.tree("append", {
                            parent: Menu.Tree.clickNode.target,
                            data: [{
                                id: result.msg,
                                text: $("[name='menuname']").val(),
                                attributes: { menulevel: Menu.Tree.level }
                            }]
                        });
                        $("#menuid").val(result.msg);
                        AR.Global.DG.action = "Update";//避免重复添加
                        AR.Window.showMsg("${add}${success}");
                    }
                    else {
                        var node = Menu.Tree.$target.tree('find', $("#menuid").val());
                        Menu.Tree.$target.tree("update", {
                            target: node.target,
                            text: $("[name='menuname']").val()
                        });
                        AR.Window.showMsg(result.msg);
                    }
                   
                    return false;
                }

            }
        };

        Menu.Tree.onInit();
        Menu.Form.onInit();


    })(window);
</script>
